/**
 * 
 */
package fr.isima.isipaint.model;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;

/**
 * @author daniel
 * 
 */
public class Scene extends Observable {

	/** The main group */
	private GraphicGroup group;

	/** The current object */
	private GraphicObject current;

	/** Insertion mode or selection */
	boolean selection;

	/** The selection */
	private GraphicObject selectionBox;
	
	/** The group of the selection*/
	private GraphicGroup groupBox;

	/** The current color used */
	private Color color;

	/** Filled Action */
	private boolean filledAction;

	/** Draw Action */
	private boolean drawAction;

	/**
	 * @param drawAction
	 *            the drawAction to set
	 */
	public final void setDrawAction(boolean drawAction) {
		this.drawAction = drawAction;
	}

	/**
	 * @return the drawAction
	 */
	public final boolean isDrawAction() {
		return drawAction;
	}

	/**
	 * @return the filledAction
	 */
	public final boolean isFilledAction() {
		return filledAction;
	}

	/**
	 * @param filledAction
	 *            the filledAction to set
	 */
	public final void setFilledAction(boolean filledAction) {
		this.filledAction = filledAction;
	}

	/**
	 * @return the color
	 */
	public final Color getColor() {
		return color;
	}

	/**
	 * @param color
	 *            the color to set
	 */
	public final void setColor(Color color) {
		this.color = color;
	}

	/* ======================================================= */
	/**
	 * Get current attribute
	 * 
	 * @return current
	 */
	/* ======================================================= */
	public final GraphicObject getCurrent() {
		return current;
	}

	/* ======================================================= */
	/**
	 * Set current with current's value
	 * 
	 * @param current
	 */
	/* ======================================================= */
	public final void setCurrent(GraphicObject current) {
		this.current = current;
	}

	/**
	 * @return the group
	 */
	public final GraphicGroup getGroup() {
		return group;
	}

	/**
	 * @return the selection
	 */
	public final boolean isSelection() {
		return selection;
	}

	/**
	 * @param selection
	 *            the selection to set
	 */
	public final void setSelection(boolean selection) {
		this.selection = selection;
	}

	/**
	 * @return the groupBox
	 */
	public final GraphicGroup getGroupBox() {
		return groupBox;
	}

	/**
	 * @param groupBox the groupBox to set
	 */
	public final void setGroupBox(GraphicGroup groupBox) {
		this.groupBox = groupBox;
		group.addGraphicObject(groupBox);
	}

	public Scene() {		
		group = new GraphicGroup();
		selectionBox = new Rectangle();	
		selectionBox.setColor(Color.RED);

	}

	public void addGraphicObject(GraphicObject object) {
		group.addGraphicObject(object);
		setChanged();
		notifyObservers();
	}

	public void removeGraphicObject(GraphicObject object) {
		group.removeGraphicObject(object);
		setChanged();
		notifyObservers();
	}

	public void resetCurrent() {
		try {
			current = current.getClass().newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

	}
	
	public void resetSelection()
	{
		selectionBox.setBounds(0, 0, 0, 0);
		setChanged();
		notifyObservers();
	}

	public GraphicObject getGraphicObject(int x, int y) {
		return group.getGraphicObject(x, y);

	}

	/**
	 * @return the selectionBox
	 */
	public final GraphicObject getSelectionBox() {
		return selectionBox;
	}

	/**
	 * @param selectionBox
	 *            the selectionBox to set
	 */
	public final void setSelectionBox(GraphicObject selectionBox) {
		this.selectionBox = selectionBox;
	}

	public void fillCurrent() {
		current.setFilled(true);
		current.setColor(color);
		setChanged();
		notifyObservers();
		
	}
	
	public void setLocationGraphicObject(GraphicObject g, int x, int y)
	{
		g.setLocation(x, y);
		setChanged();
		notifyObservers();
	}
	
	public void setBoundsGraphicObject(GraphicObject g, int x, int y, int width, int height)
	{
		g.setBounds(x, y, width, height);
		setChanged();
		notifyObservers();
	}

	public void reset() {		
		group.removeAll();
		group.getGroup().clear();
		setChanged();
		notifyObservers();
		
	}	

}
